package com.hula.onion.book.server.spider.platforms.shoujinwang;

import com.alibaba.fastjson.JSONObject;
import com.hula.onion.book.server.common.CommonResponse;
import com.hula.onion.book.server.common.SpiderCommon;
import com.hula.onion.book.server.config.ResponseStatusEnum;
import com.hula.onion.book.server.utils.CookieUtils;
import com.hula.onion.book.server.utils.HttpUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.message.BasicHeader;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Description:元宝365
 * Create Date:2018/1/30 19:33
 *
 * @author liangbing
 * @version 1.0
 */
@Component("shouJinWangSpider")
public class ShouJinWangSpider extends SpiderCommon {

    /**
     * 登录页面URL
     */
    private static final String LOGIN_PAGE_URL = "https://www.shoujinwang.com/systLogonUser/login.do";

    /**
     * 登录URL
     */
    private static final String LOGIN_URL = "https://www.shoujinwang.com/systLogonUser/adminLogon.do";

    /**
     * 验证码URL
     */
    private static final String CAPTCHA_URL = "https://www.shoujinwang.com/systLogonUser/getImage.do";

    /**
     * 获取账户信息URL
     */
    private static final String GET_ACCOUNT_INFO_URL = "https://www.shoujinwang.com/personal/system/personal/indexData";

    @Autowired
    private ShouJinWangSpiderHelper spiderHelper;

    /**
     * 执行爬取操作
     */
    @Override
    public CommonResponse execute() {
        //1. 从缓存中获取cookie，设置到cookie store中
        CookieUtils.setCookie(loginForm.getPlatformId(), loginForm.getUserId(), getHttpContext().getCookieStore());

        //模拟登陆
        BasicHeader basicHeader = new BasicHeader("Referer", "https://www.shoujinwang.com/systLogonUser/login.do");

        String loginStr = HttpUtils.post(LOGIN_URL,
                spiderHelper.installRequestParams(loginForm),
                getHttpContext(), basicHeader);
        if (StringUtils.isEmpty(loginStr)) {
            return new CommonResponse(ResponseStatusEnum.FAIL);
        }
        logger.info("[ShouJinWangSpider] Http response result:{}", loginStr);

        boolean status = false;
        String msg = "";
        try {
            status = JSONObject.parseObject(loginStr).getBoolean("success");
        } catch (Exception e) {
            Document dc = Jsoup.parse(loginStr);
            msg = dc.selectFirst("p.fs18").text();
        }
        System.out.println(loginStr);
        //登陆成功
        if (status) {
            String jsonStr = HttpUtils.get(GET_ACCOUNT_INFO_URL, getHttpContext());
            return spiderHelper.parseAccountBalance(jsonStr);
        } else {
            CommonResponse commonResponse = new CommonResponse(ResponseStatusEnum.ACCOUNT_ERROR);

            commonResponse.setMsg(msg);

            return commonResponse;
        }

    }

    @Override
    public String getCaptchaURL() {
        return CAPTCHA_URL;
    }

    @Override
    public String getLoginPageURL() {
        return LOGIN_PAGE_URL;
    }

}

